﻿@{
    ViewData["Title"] = "Home Page";
}
<link href="~/lib/zTree/css/bootstrapStyle/bootstrapStyle.css" rel="stylesheet" />
<script src="~/lib/zTree/js/jquery.ztree.core.js"></script>
<script src="~/lib/zTree/js/jquery.ztree.excheck.js"></script>
<script src="~/lib/zTree/js/jquery.ztree.exhide.js"></script>
<script src="~/lib/zTree/js/jquery.ztree.all.js"></script>

<div id="main">
    <ul class="layui-nav">
        <li class="layui-nav-item">
            <a id="Generator" href="javascript:void(0)">生成代码</a>
        </li>
        <li class="layui-nav-item" style="float:right">提伯斯代码生成器v1.0</li>
    </ul>
    <div id="menu-left" class="menu-left">
        <div class="layui-form left-border">
            <ul id="tree" class="ztree"></ul>
        </div>
    </div>
    <div id="temp" class="temp">
        <div class="layui-form">
            <ul id="tree2" class="ztree"></ul>
        </div>
    </div>
</div>

<style>
    /*修改滚动条样式*/
    div::-webkit-scrollbar{
      width:10px;
      height:10px;
      /**/
    }
    div::-webkit-scrollbar-track{
      background: rgb(239, 239, 239);
      border-radius:2px;
    }
    div::-webkit-scrollbar-thumb{
      background: #bfbfbf;
      border-radius:10px;
    }
    div::-webkit-scrollbar-thumb:hover{
      /*background: #333;*/
    }
    div::-webkit-scrollbar-corner{
      background: rgb(239,239,239);
    }
    html body {

       margin:0 auto;
       margin-top:80px;
       width:900px;
       border-bottom:10px solid rgb(239,239,239);
    }
    .menu-left {
        float:left;
        width: 50%;
        height: 600px;
        overflow-y: scroll;
    }
    .left-border {
        height: 100%;
        border-left: 10px solid rgb(239,239,239);
    }
    .temp{
        width:50%;
        height: 600px;
        overflow-y: scroll;
    }
</style>

<script type="text/javascript">

    var setting = {
        view: {
            addHoverDom: addHoverDom,
            selectedMulti: false
        },
        edit: {
            enable: true,
            //给节点额外增加属性来控制“重命名”、“删除”图标的显示或隐藏

            showRenameBtn: showRenameBtn,
            showRemoveBtn: showRemoveBtn
         },
         check: {
　　　　    enable: true,   //true / false 分别表示 显示 / 不显示 复选框或单选框
　　　　    autoCheckTrigger: true,   //true / false 分别表示 触发 / 不触发 事件回调函数
            chkStyle: "checkbox",   //勾选框类型(checkbox 或 radio）
        },
        data: {
            simpleData: {
            enable: true
            }
        },
        callback: {
            beforeDrag: beforeDrag,
            beforeEditName: beforeEditName,
            beforeRemove: beforeRemove,
            beforeRename: beforeRename,
            onRemove: onRemove,
            onRename: onRename,
            beforeClick: beforeClick,
        }
    };

    var setting2 = {
        view: {
            selectedMulti: false
        },
        edit: {
            enable: true,
            //给节点额外增加属性来控制“重命名”、“删除”图标的显示或隐藏
            showRenameBtn: showRenameBtn,
            showRemoveBtn: showRemoveBtn
         },
         check: {
　　　　    enable: true,   //true / false 分别表示 显示 / 不显示 复选框或单选框
　　　　    autoCheckTrigger: true,   //true / false 分别表示 触发 / 不触发 事件回调函数
　　　　    chkStyle: "checkbox",   //勾选框类型(checkbox 或 radio）
        },
        data: {
            simpleData: {
            enable: true
            }
        },
        callback: {
            beforeDrag: beforeDrag,
            beforeRename: beforeRename,
            onRemove: onRemove,
            onRename: onRename,
        }
    };



    var zNodes;
    var log, className = "dark";
    var treeNodeEdit;
    var checkNode;
    function beforeDrag(treeId, treeNodes) {
        return false;
    }

    function ZtreeNode(id, pId, name) {//定义ztree的节点类
        this.id = id;
        this.pId = pId;
        this.name = name;
        this.noRemoveBtn = true;
        this.noEditBtn = true;
}    

    //选中事件
    function beforeClick(treeId, treeNode) {

        if (treeNode.id != "0" && treeNode.children == null) {
            checkNode = treeNode;
            var zTree = $.fn.zTree.getZTreeObj("tree");
            if (treeNode.level == 1) {
                //异步加载
                $.ajax({
                    url: '/home/GetServerByID',
                    type: 'post',
                    async: false,//使用同步的方式,true为异步方式
                    data: { Id: treeNode.id },//这里使用json对象
                    success: function (data) {
                        console.log(data);
                        if (data.code == 200) {
                            var arr = [];
                            $.each(data.data, function (k, v) {
                                var childZNode = new ZtreeNode(v.id, v.pId, v.name); //构造子节点
                                arr.push(childZNode);
                            })
                            zTree.addNodes(treeNode, arr, true);
                        }
                    }
                });
            }
            //else {
            //    //异步加载
            //    $.ajax({
            //        url: '/home/GetDbTable',
            //        type: 'post',
            //        async: false,//使用同步的方式,true为异步方式
            //        data: { Id: treeNode.pId,dbName:treeNode.name },//这里使用json对象
            //        success: function (data) {
            //            if (data.code == 200) {
            //                var arr = [];
            //                $.each(data.data, function (k, v) {
            //                    var childZNode = new ZtreeNode(v.Id,treeNode.id, v.TableName); //构造子节点
            //                    arr.push(childZNode);
            //                })
            //                zTree.addNodes(treeNode, arr, true);
            //                console.log(data.data)
            //            }
            //        }
            //    });
            //}
        } else {
            checkNode = null;
        }
    }


    function beforeEditName(treeId, treeNode) {
        console.log(treeNode)
        className = (className === "dark" ? "" : "dark");
        showLog("[ " + getTime() + " beforeEditName ]&nbsp;&nbsp;&nbsp;&nbsp; " + treeNode.name);
        var zTree = $.fn.zTree.getZTreeObj("tree");
        zTree.selectNode(treeNode);
        treeNodeEdit = treeNode;
        var url = "/home/edit?Id=" + treeNode.id;
        console.log(treeNode);
        if (treeNode.pId == "temp0")
        {
            url = "/TemplateConfig/edit?Id="+ treeNode.id;
        }


        layui.use('layer', function () {
            var layer = layui.layer;
            layer.open({
                type: 2,
                area: ['650px', '500px'],
                fixed: false, //不固定
                maxmin: true,
                title: '编辑',
                content: url,
                btn: ['确定', '取消'],
                yes: function (index, layero) {
                    $("#form").length;//直接获取表单长度=0
                    $(layero).find("#form").length;//表单长度还是等于0
                    var submit = layero.find('iframe').contents().find("#layuiadmin-app-form-edit");
                    submit.click();
                }
            });
        });
        return false;
    }


    function beforeEditName2(treeId, treeNode) {
        console.log(treeNode)
        showLog("[ " + getTime() + " beforeEditName ]&nbsp;&nbsp;&nbsp;&nbsp; " + treeNode.name);
        var zTree = $.fn.zTree.getZTreeObj("tree");
        zTree.selectNode(treeNode);
        treeNodeEdit = treeNode;
        var url = "?Id=" + treeNode.id;
        layui.use('layer', function () {
            var layer = layui.layer;
            layer.open({
                type: 2,
                area: ['600px', '400px'],
                fixed: false, //不固定
                maxmin: true,
                title: '编辑',
                content: '/TemplateConfig/edit' + url,
                btn: ['确定', '取消'],
                yes: function (index, layero) {
                    $("#form").length;//直接获取表单长度=0
                    $(layero).find("#form").length;//表单长度还是等于0
                    var submit = layero.find('iframe').contents().find("#layuiadmin-app-form-edit");
                    submit.click();
                }
            });
        });
        return false;
    }



    //回调,修改节点
    function editNode(data) {
        var zTree = $.fn.zTree.getZTreeObj("tree");
        treeNodeEdit.name = data.FullName;
        treeNodeEdit.noEditBtn = false;
        treeNodeEdit.noRemoveBtn = false;
        zTree.updateNode(treeNodeEdit);
    }

    function editNode2(data) {
        var zTree = $.fn.zTree.getZTreeObj("tree2");
        treeNodeEdit.name = data.Name;
        treeNodeEdit.noEditBtn = false;
        treeNodeEdit.noRemoveBtn = false;
        zTree.updateNode(treeNodeEdit);
    }


    function beforeRemove(treeId, treeNode, isDel) {
        layui.use('layer', function () {
            var url = "/home/deleteServer"
            var para = { Id: treeNode.id };
            console.log(treeNode);
            if (treeNode.pId == "temp0")
            {
                zTree = $.fn.zTree.getZTreeObj("tree2");
                url = "/TemplateConfig/deletetemp";
            }
            zTree.selectNode(treeNode);
            var layer = layui.layer;
            layer.confirm('确认要删除吗？', {
                btn: ['确定', '取消']
            }, function (index) {
                layer.close(index);
                $.ajax({
                    url: url,
                    type: 'post',
                    async: false,//使用同步的方式,true为异步方式
                    data: para,//这里使用json对象
                    success: function (data) {
                        if (data.code == 200) {
                            zTree.removeNode(treeNode);
                        }
                    }
                });
            }, function (index) {

            });
        });
        return false;
    }


    function onRemove(e, treeId, treeNode) {
        showLog("[ " + getTime() + " onRemove ]&nbsp;&nbsp;&nbsp;&nbsp; " + treeNode.name);
    }
    function beforeRename(treeId, treeNode, newName) {
        className = (className === "dark" ? "" : "dark");
        showLog("[ " + getTime() + " beforeRename ]&nbsp;&nbsp;&nbsp;&nbsp; " + treeNode.name);
        if (newName.length == 0) {
            alert("节点名称不能为空.");
            var zTree = $.fn.zTree.getZTreeObj("tree");
            setTimeout(function () { zTree.editName(treeNode) }, 10);
            return false;
        }
        return true;
    }
    function onRename(e, treeId, treeNode) {
        showLog("[ " + getTime() + " onRename ]&nbsp;&nbsp;&nbsp;&nbsp; " + treeNode.name);
    }
    function showLog(str) {
        if (!log) log = $("#log");
        log.append("<li class='" + className + "'>" + str + "</li>");
        if (log.children("li").length > 8) {
            log.get(0).removeChild(log.children("li")[0]);
        }
    }
    function getTime() {
        var now = new Date(),
            h = now.getHours(),
            m = now.getMinutes(),
            s = now.getSeconds(),
            ms = now.getMilliseconds();
        return (h + ":" + m + ":" + s + " " + ms);
    }


    //是否显示编辑按钮
    function showRenameBtn(treeId, treeNode) {
        //获取节点所配置的noEditBtn属性值
        if (treeNode.noEditBtn != undefined && treeNode.noEditBtn) {
            return false;
        } else {
            return true;
        }
    }
    //是否显示删除按钮
    function showRemoveBtn(treeId, treeNode) {
        //获取节点所配置的noRemoveBtn属性值
        if (treeNode.noRemoveBtn != undefined && treeNode.noRemoveBtn) {
            return false;
        } else {
            return true;
        }
    }
    var newCount = 1;
    var treeNodeAdd; //用来记录当前添加的节点对象

    //添加
    function addHoverDom(treeId, treeNode) {
        if (treeNode.level > 0) {
            return false;
        } else {
            //给节点添加"新增"按钮
            var sObj = $("#" + treeNode.tId + "_span");
            if (treeNode.editNameFlag || $("#addBtn_" + treeNode.id).length > 0) return;
            var addStr = "<span class='button add' id='addBtn_" + treeNode.id
                + "' title='add node' onfocus='this.blur();'></span>";
            sObj.after(addStr);
            var btn = $("#addBtn_" + treeNode.id);
            if (btn) btn.bind("click", function () {
                
                treeNodeAdd = treeNode;
                var url = "/home/create";
                if (treeNode.id == "temp0")
                {
                    url = "/TemplateConfig/create";
                }
                layui.use('layer', function () {
                    var layer = layui.layer;
                    layer.open({
                        type: 2,
                        area: ['650px', '500px'],
                        fixed: false, //不固定
                        maxmin: true,
                        content: url,
                        title: '添加',
                        btn: ['确定', '取消'],
                        yes: function (index, layero) {
                            $("#form").length;//直接获取表单长度=0
                            $(layero).find("#form").length;//表单长度还是等于0
                            var submit = layero.find('iframe').contents().find("#layuiadmin-app-form-submit");
                            submit.click();
                        }
                    });
                });

                return false;
            });
        }

    };

    //回调,添加节点
    function addNode(data) {
        var zTree = $.fn.zTree.getZTreeObj("tree");
        zTree.addNodes(treeNodeAdd, { id: data.Id, pId: treeNodeAdd.id, name: data.FullName, noEditBtn: false, noRemoveBtn: false });
    }


    function addNode2(data) {
        var zTree = $.fn.zTree.getZTreeObj("tree2");
        zTree.addNodes(treeNodeAdd, { id: data.Id, pId: treeNodeAdd.id, name: data.Name, noEditBtn: false, noRemoveBtn: false });
    }


    function selectAll() {
        var zTree = $.fn.zTree.getZTreeObj("tree");
        zTree.setting.edit.editNameSelectAll = $("#selectAll").attr("checked");
    }

    $(document).ready(function () {

        var winHeight = $(window).height() - 60;

        layui.use('layer', function () {
            layer = layui.layer;
        });
        $.ajax({
            url: '/Home/GetTreeDBList',
            type: 'post',
            async: false,//使用同步的方式,true为异步方式
            data: {},//这里使用json对象
            success: function (data) {
                zNodes = data.data;
            }
        });
        zTree = $.fn.zTree.init($("#tree"), setting, zNodes);


        $.ajax({
            url: '/TemplateConfig/GetTempList',
            type: 'post',
            async: false,//使用同步的方式,true为异步方式
            data: {},//这里使用json对象
            success: function (data) {
                zNodes = data.data;
            }
        });
        zTree2 = $.fn.zTree.init($("#tree2"), setting, zNodes);

        $("#Generator").click(function ()
        {
            var selectedNodes = zTree.getCheckedNodes();
            var selectedNodes2 = zTree2.getCheckedNodes();
            var tables = [];
            var temps = [];
            $.each(selectedNodes, function (k, v) {
                if (v.level == 3)
                {
                    tables.push(v.id);
                }
            });
            $.each(selectedNodes2, function (k, v) {
                if (v.level == 1)
                {
                    temps.push(v.id);
                }
            });
            if (tables.length == 0 || temps.length == 0) {
                layui.use('layer', function () {
                    layer = layui.layer;
                    layer.msg("请选择生成的表跟模板!");
                });

            } else {
                $.ajax({
                url: '/Home/Generator',
                type: 'post',
                async: false,//使用同步的方式,true为异步方式
                data: {tables:tables,temps:temps},//这里使用json对象
                success: function (data) {
                    layui.use('layer', function () {
                    layer.msg(data.msg);
                });
                }
            });
            }

        })
    });

</script>
